package com.mynovamobile.android;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class MNMReauthorizationController {
	private static final String TAG = "[MNMReauthorizationController]";
	private LinkedList<Intent> authorizationSteps;
	private Activity context;
	
	public MNMReauthorizationController(Activity context) {
		this.context = context;
		authorizationSteps = new LinkedList<Intent>();
	}
	
	public MNMReauthorizationController addAuthStep(Intent step) {
		authorizationSteps.add(step);
		return this;
	}
	
	public Intent getNextStep() {
		return authorizationSteps.removeFirst();
	}
	
	public int getStepCount() {
		return authorizationSteps.size();
	}
	
	public boolean needsReauthorization() {
		MNMUserDefaults userData = MNMUserDefaults.getInstance(context);

		if (userData.getLDAPUsername() == null || userData.getLDAPPassword() == null) {
			// TODO: Change this intent to be a ldap registration
			Log.v(TAG, "ldap credentials need reauth");
			// addAuthStep(new Intent(context.getApplicationContext(), null));
		} else {
			// TODO: Validate Stored data with server.
		}
		
		if (userData.getNovasisPIN() == null || userData.getStudentID() == null) {
			// TODO: Change this intent to be a Novasis registration
			Log.v(TAG, "novasis credentials need reauth");
			addAuthStep(new Intent(context.getApplicationContext(), LDAPLogin.class));
		} else {
			// TODO: Validate Stored data with server.
		}
		
		if (userData.getWildcardID() == null || userData.getWildcardPassword() == null) {
			// TODO: Change this intent to be a Wildcard registration
			Log.v(TAG, "wildcard credentials need reauth");
			addAuthStep(new Intent(context.getApplicationContext(), null));
		} else {
			// TODO: Validate Stored data with server.
		}
		
		return getStepCount() > 0;
	}
	
	public void reauthorize() {
		if (getStepCount() > 0) {
			moveToNextStep();
		}
	}
	
	private void moveToNextStep() {
		Log.v(TAG, "STARTED NEW REAUTHORIZATION ACTIVITY");
		Intent currentStep = getNextStep();
		//context.startActivity(currentStep);
	}
}
